﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LogFileParser;

namespace LogFileViewer
{
    public partial class Filter : Form
    {
        private string _filterProperty = "";
        private LogFileParser.LogFileParser _lfp;
        private List<LogFileItem> _filteredList;
        public Filter()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();            
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            FilteredList = _lfp.Filter(cbField1.Text, cbValue.Text,cbrelation.Text);
            this.Close();  
        }

        public string FilterProperty
        {
            get { return _filterProperty; }
            set { _filterProperty = value; }
        }

        public LogFileParser.LogFileParser LogFile
        {
            get { return _lfp; }
            set { _lfp = value; }
        }

        public List<LogFileItem> FilteredList
        {
            get { return _filteredList; }
            private set { _filteredList = value; }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Filter_Load(object sender, EventArgs e)
        {
            foreach (string field in _lfp.Fields)
            {
                this.cbField1.Items.Add(field);
            }
        }

        private void cbField1_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<string> values = _lfp.GetUniqueValues(cbField1.Text);
            cbValue.DataSource = values;
            cbValue.Refresh();
        }

        private void cbrelation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbrelation.Text != "")
            {
                btnFilter.Enabled = true;
            }
            else
            {
                btnFilter.Enabled = false;
            }
        }
    }
}
